#include "../head/predictlineform.h"
#include "ui_predictlineform.h"
#include <QtCharts>
#include <QtCharts/QChartView>
#include <QtCharts/QChart>
#include <QtCharts/QLegend>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QtCore/QTime>
#include <QtWidgets/QGridLayout>
#include <QtCharts/QScatterSeries>
#include "../head/ddmsform.h"
//#include "../head/utils/databaseutil.h"
#include"math.h"
#include "../head/mainwindow.h"

extern DdmsForm *ddms; //导向钻进DDMS
PredictLineForm::PredictLineForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PredictLineForm)
{
    ui->setupUi(this);
    this->setWindowTitle("设计曲线");
    //绘制施工曲线点图

   drawDesignline();
}


void PredictLineForm::drawDesignline(){
    //初始化散点图
    series1 = new QScatterSeries();              // 创建一个散点数据集对象
    series1->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
    series1->setMarkerSize(10);
    series1->setName("俯视图设计曲线");

    chart1 = new QChart();
    axisX1 = new QValueAxis();
    axisY1 = new QValueAxis();
    axisX1->setMin(0);
    axisY1->setMin(-20);
    axisX1->setMax(160);
    axisY1->setMax(20);
    axisY1->setReverse(true);                      //坐标翻转
    axisX1->setLabelFormat("%d");
    axisY1->setLabelFormat("%0.1f");
    chart1->addAxis(axisY1, Qt::AlignLeft);                      // 将X轴添加到图表上
    chart1->addAxis(axisX1, Qt::AlignTop);                    // 将Y轴添加到图表上
    chart1->legend()->setVisible(true);               // 隐藏图例
 //  m_axisX->setTitleText("X-label");
 //  m_axisY->setTitleText("Y-label");
    chart1->addSeries(series1);                              // 将曲线对象添加到图表上
    series1->attachAxis(axisX1);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
    series1->attachAxis(axisY1);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
    chartView1 = new QChartView(chart1);
    //chartView1->chart()->setTitle("设计曲线与施工曲线");
    ui->gridLayoutLine_2->addWidget(chartView1, 1, 1);



    //初始化散点图
    series6 = new QScatterSeries();              // 创建一个散点数据集对象
    series6->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
    series6->setMarkerSize(10);
    series6->setName("侧视图设计曲线");

    chart4 = new QChart();
    axisX2 = new QValueAxis();
    axisY2 = new QValueAxis();
    axisX2->setMin(0);
    axisY2->setMin(0);
    axisX2->setMax(160);
    axisY2->setMax(20);
    axisY2->setReverse(true);                      //坐标翻转
    axisX2->setLabelFormat("%d");
    axisY2->setLabelFormat("%0.1f");
    chart4->addAxis(axisY2, Qt::AlignLeft);                      // 将X轴添加到图表上
    chart4->addAxis(axisX2, Qt::AlignTop);                    // 将Y轴添加到图表上
    chart4->legend()->setVisible(true);               // 隐藏图例
 //  m_axisX->setTitleText("X-label");
 //  m_axisY->setTitleText("Y-label");
    chart4->addSeries(series6);                              // 将曲线对象添加到图表上
    series6->attachAxis(axisX2);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
    series6->attachAxis(axisY2);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后

    chartView2 = new QChartView(chart4);
    //chartView2->chart()->setTitle("设计曲线与施工曲线");
    ui->gridLayoutLine_3->addWidget(chartView2, 1, 1);

   initDataList();


}


//绘制施工曲线点图
void PredictLineForm::drawLineChart(){



}

void PredictLineForm::initDataList(){

    QString fileName = QApplication::applicationDirPath() + "/conf/InitParam.ini";

    QFile file1(fileName);
    QStringList  user_data;
    double ZGCD = mutiSignalDecode->sys_zgcd[HIGH];
    ui->zgcd->setText(QString::number(ZGCD,'f',1));
    ui->zgcd->setReadOnly(true);


    //读取设计曲线信息
    fileName = QApplication::applicationDirPath() + "/conf/shejiquxian.txt";
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly)){
        qDebug()<<"打开失败";
    }

    QTextStream in(&file);

    int i = 0;

    series1->clear();
    series6->clear();
    design_x2 = 0;
    design_y2 = 0;
    design_z2 = 0;

    while(!in.atEnd()){
        QString line = in.readLine();
        user_data = line.split("|");

        i++;
        design_x2 = design_x2 + ZGCD * cos(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);     
        design_y2 = design_y2 + ZGCD * sin(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
        design_z2 = design_z2 + ZGCD * cos(user_data[3].toFloat()*PI/180);

        series1->append(design_x2,design_y2);//俯视图
        series6->append(design_x2,design_z2);//侧视图

    }
    file.close();

}

PredictLineForm::~PredictLineForm()
{
    delete ui;
}

void PredictLineForm::on_pushButton_7_clicked()
{
    double courseAngle = ui->fwj->text().toDouble();
    double pitchAngle = ui->qj->text().toDouble();
    double drillLength = ui->zgcd->text().toDouble();

    design_x2 = design_x2 + drillLength * cos(courseAngle*PI/180) * sin(pitchAngle*PI/180);
    design_y2 = design_y2 + drillLength * sin(courseAngle*PI/180) * sin(pitchAngle*PI/180);
    design_z2 = design_z2 + drillLength * cos(pitchAngle*PI/180);

    series1->append(design_x2,design_y2);//俯视图
    series6->append(design_x2,design_z2);//侧视图

    //rf->add_design(ui->fwj->text(),ui->qj->text(),ui->zgcd->text());
}
